<template>
	<view class="template-product tn-safe-area-inset-bottom">
		<!-- 顶部自定义导航 -->
		<tn-nav-bar fixed alpha customBack>
			<view slot="back" class='tn-custom-nav-bar__back' @click="goBack">
				<text class='icon tn-icon-left'></text>
				<text class='icon tn-icon-home-capsule-fill'></text>
			</view>
		</tn-nav-bar>

		<swiper class="card-swiper" :circular="true" :autoplay="true" duration="500" interval="5000" @change="cardSwiper">
			<swiper-item v-for="(item,index) in swiperList" :key="index" :class="cardCur==index?'cur':''">
				<view class="swiper-item image-banner">
					<image :src="item" mode="aspectFill"></image>
				</view>
			</swiper-item>
		</swiper>
		<view class="indication">
			<block v-for="(item,index) in swiperList" :key="index">
				<view class="spot" :class="cardCur==index?'active':''"></view>
			</block>
		</view>

		<view class="tn-margin">
			<view class="tn-flex tn-flex-row-between">
				<view class="justify-content-item tn-text-bold tn-margin-top-xs" style="font-size: 38rpx;">
					{{collectionContent.title || ""}}
				</view>
			</view>
			<view class="tn-flex tn-flex-row-start tn-margin-top">
				<view class="justify-content-item tn-color-gray">
					{{collectionContent.remark || ""}}
				</view>
			</view>
			<view class="tn-flex tn-flex-row-start tn-margin-top">
				<!-- <view class="justify-content-item tn-text-bold tn-color-purplered">
					<text class="" style="font-size: 50rpx;">会员免费</text>
					<text class="tn-padding-left-xs" style="font-size: 30rpx;">￥</text>
				</view> -->
				<view class="justify-content-item tn-color-gray tn-padding-top-xs">
					<view class="">答题次数 {{collectionContent.submit || 0 }}</view>
				</view>
				<!-- 	<view class="justify-content-item tn-color-gray tn-padding-top-xs" style="padding-left: 10rpx;">
					<view class="">收藏人数 {{collectionContent.collect || 0 }}</view>
				</view> -->
			</view>
		</view>

		<!-- 边距间隔 -->
		<view class="tn-strip-bottom"></view>


		<!-- 更多信息-->
		<view class="tn-padding-top-sm tn-padding-bottom-sm">
			<tn-list-cell :hover="true" :unlined="true" :radius="true" :fontSize="30">
				<view class="tn-flex tn-flex-col-center">
					<view class="tn-flex-1">试题类目</view>
					<view class="tn-margin-left-sm" style="font-size: 28rpx;">
						{{collectionContent.category.title || ""}}
					</view>
				</view>
			</tn-list-cell>
			<tn-list-cell :hover="true" :unlined="true" :radius="true" :fontSize="30">
				<view class="tn-flex tn-flex-col-center">
					<view class="tn-flex-1">试题难度</view>
					<block v-if="collectionContent.level == 1">
						<view class="tn-margin-left-sm" style="font-size: 28rpx;">简单</view>
					</block>
					<block v-if="collectionContent.level == 2">
						<view class="tn-margin-left-sm" style="font-size: 28rpx;">较难</view>
					</block>
					<block v-if="collectionContent.level == 3">
						<view class="tn-margin-left-sm" style="font-size: 28rpx;">中等</view>
					</block>
					<block v-if="collectionContent.level == 4">
						<view class="tn-margin-left-sm" style="font-size: 28rpx;">困难</view>
					</block>
				</view>
			</tn-list-cell>
			<tn-list-cell :hover="true" :unlined="true" :radius="true" :fontSize="30">
				<view class="tn-flex tn-flex-col-center">
					<view class="tn-flex-1">试题来源</view>
					<view class="tn-margin-left-sm" style="font-size: 28rpx;">{{collectionContent.author || ""}}
					</view>
				</view>
			</tn-list-cell>
		</view>

		<view class="tn-strip-bottom"></view>

		<!-- 答题人头像图开始 -->
		<!-- <view class="tn-margin-top tn-padding-top-sm tn-margin-bottom">
			<view class="see">
				<view class="justify-content-item tn-flex tn-flex-col-center">
					<view style="margin-left: 15rpx;margin-right: 6rpx;transform: scale(0.85);">
						<tn-avatar-group :lists="groupAvatarList" size="lg"></tn-avatar-group>
					</view>
					<text class="tn-color-gray" style="font-size: 30rpx;">{{groupList.count}}人</text>
				</view>
				<view class="justify-content-item tn-color-gray tn-text-center tn-margin-right"
					style="display: flex;align-items: center;font-size: 30rpx;">
					<text class="tn-icon-write tn-text-lg"
						:style="groupList.is_submit ? 'padding-right: 5rpx;color: red;': ''"></text>
					<text class="tn-padding-right tn-text-df">{{collectionContent.submit}}</text>
					<text class="tn-icon-like-lack tn-text-lg" @click="submitCollection"
						:style="groupList.is_collection ? 'padding-right: 5rpx;color: red;': ''"></text>
					<text class="tn-text-df" @click="submitCollection">{{collectionContent.collect}}</text>
				</view>
			</view>
		</view> -->
		<!-- 答题人头像组结束 -->
		<!-- 边距间隔 -->

		<!-- 产品标签开始 -->
		<!-- <view class="tn-margin">
      <view class="tn-flex tn-flex-row-between">
        <view class="justify-content-item tn-text-bold tn-text-xl">
          产品标签
        </view>
      </view>
    </view>
    
    <view class="">
      <view class="tn-tag-content tn-margin tn-text-justify">
        <view v-for="(item, index) in tagList" :key="index" class="tn-tag-content__item tn-margin-right tn-round tn-text-sm tn-text-bold" :class="[`tn-bg-${item.color}--light tn-color-${item.color}`]">
          <text class="tn-tag-content__item--prefix">#</text> {{ item.title }}
        </view>
      </view>
    </view> -->
		<!-- 产品标签结束 -->

		<!-- 信息流广告开始 -->
		<view class="" v-if="showAd">
			<view class="tn-tag-content tn-margin tn-text-justify">
				<ad unit-id="adunit-65c56a85e788fe7b"></ad>
			</view>
			<view class="tn-strip-bottom"></view>
		</view>
		<!-- 信息流广告结束 -->
		
		
		<block v-if="rankList.length > 0">
			<!-- 试卷排行开始-->
			<view class="tn-padding-top-sm tn-padding-bottom-sm">
				<view class="tn-margin">
					<view class="tn-flex tn-flex-row-between">
						<view class="justify-content-item tn-text-bold tn-text-xl">
							答题排行
						</view>
						<view class="justify-content-item tn-margin-right tn-text-df tn-color-gray">
							<text class="tn-padding-xs">该数据根据历史答题记录统计</text>
						</view>
					</view>
				</view>
				<block v-for="(item, index) in rankList" :key="index">
					<view class="">
						<tn-list-cell :hover="true" :unlined="true" :radius="true" :fontSize="30">
							<view class="tn-flex tn-flex-col-center">
								<view class="icon1__item--icon tn-flex tn-flex-row-center tn-flex-col-center">
									<view class="" style="display: flex;align-items: center;">
										<text style="padding-right: 10rpx;">第{{ index + 1 }}名</text>
										<img :src="item.avatar" alt="" srcset=""
											style="width: 50rpx;height: 50rpx;border-radius: 50%;">
									</view>
								</view>
								<view class="tn-margin-left-sm tn-flex-1">{{item.nickname}}</view>
								<view class="">{{item.score.toFixed(2) + ` 分`}}</view>
							</view>
						</tn-list-cell>
					</view>
				</block>
			</view>
			<view class="tn-strip-bottom"></view>
			<!-- 试卷排行结束 -->
		</block>

		<view class="tn-margin-top-sm">
			<view class="nav_title--wrap">
				<view class="nav_title tn-cool-bg-color-6">
					<text class="tn-icon-star tn-padding-right-sm tn-text-xxl"></text>
					<text class="tn-text-xl">兔兔答题 · 试题简介</text>
					<text class="tn-icon-star tn-padding-left-sm tn-text-xxl"></text>
				</view>
			</view>
		</view>

		<view class="content-backgroup tn-margin" style="padding-bottom:85px;">
			<mp-html :content="collectionContent.content" />
		</view>

		<view id="top-info" class="tn-footerfixed tn-safe-area-inset-bottom tn-bg-white"
			:style="{transform: `translateY(${topInfoTranslateY}px)`}">
			<view class="tn-flex tn-flex-row-between tn-flex-col-center">
				<view class="tn-flex-2 justify-content-item tn-color-gray">
					<view class="tn-flex tn-flex-row-center">
						<view class="tn-flex-1" @click="collect">
							<view class="tn-icon-like" style="font-size: 42rpx;"></view>
							<view class="tn-text-center tn-text-sm tn-padding-top-xs">收藏</view>
						</view>
						<view class="tn-flex-1 tn-padding-right-xl" @click="subscribe">
							<button class="tn-button--clear-style" style="border-radius: 0;">
								<view class="tn-icon-notice" style="font-size: 42rpx;"></view>
								<view class="tn-text-center tn-text-sm tn-padding-top-xs">订阅</view>
							</button>
						</view>
					</view>
				</view>
				<view class="tn-flex-3 justify-content-item tn-padding-top-sm tn-padding-bottom-sm">
					<tn-button shape="round" backgroundColor="tn-main-gradient-blue" openType="share" padding="38rpx 0"
						width="40%" :fontSize="30" shadow>
						<text class="tn-color-white" hover-class="tn-hover" :hover-stay-time="150">
							邀请好友
						</text>
					</tn-button>
					<tn-button shape="round" @click="onlineExam(collectionContent.uid)" backgroundColor="#27B93A"
						padding="38rpx 0" margin="0 0 0 30rpx" width="40%" :fontSize="30" shadow>
						<text class="tn-color-white" hover-class="tn-hover" :hover-stay-time="150">
							立即答题
						</text>
					</tn-button>
				</view>
			</view>
		</view>

		<view class='tn-tabbar-height'></view>

	</view>
</template>

<script>
	import template_page_mixin from '@/libs/mixin/template_page_mixin.js'
	import {
		getCollectionContent,
		submitCollection,
		getRankListByCollection
	} from "@/utils/api/exam.js"
	import {
		getUserBasicInfoNoAuth
	} from "@/utils/api/user.js"
	export default {
		name: 'TemplateProduct',
		mixins: [template_page_mixin],
		data() {
			return {
				cardCur: 0,
				swiperList: [],
				tagList: [{
					color: 'red',
					title: "真皮",
				}],
				topInfo: {
					height: 0,
				},
				topInfoTranslateY: 0,
				prevScrollTop: 0,
				collectionUid: "",
				groupList: [], // 头像原生数据
				groupAvatarList: [], //头像组装数据
				collectionContent: {},
				rankList: [],
				showAd: getApp().globalData.showAd
			}
		},
		onReady() {
			this.getTopInfoRect()
		},
		onPageScroll(e) {
			this.handlePageScroll(e.scrollTop)
		},
		onLoad(params) {
			this.collectionUid = params.uid || ""
			this.getCollectionContent()
			this.getSubmitRankList()
			this.$t.mpShare = {
				title: this.collectionContent.title,
				path: "/subpages/exam/collection/detail?uid=" + this.collectionUid,
				imageUrl: this.collectionContent.url + this.collectionContent.path,
				share: true
			}
			if (!this.$t.mpShare.share) {
				uni.hideShareMenu()
			}
		},
		methods: {
			// 试卷收藏
			collect() {
				this.$func.showToast("努力开发中")
			},
			// 获取答题排行
			getSubmitRankList() {
				getRankListByCollection({
					uid: this.collectionUid,
					page: 1,
					size: 20
				}).then(res => {
					if (res.code == 100) {
						this.rankList = res.data.items
						return
					}
					this.$func.showTotast(res.msg)
				})
			},
			// 试卷收藏操作
			submitCollection() {
				if (this.groupList.is_collection) {
					this.$func.showToast("你已收藏")
					return
				}
				submitCollection({
					uid: this.collectionUid
				}).then(res => {
					if (res.code == 100) {
						this.groupList.is_collection = true
						this.groupList.collect = this.groupList.collect + 1
						this.collectionContent.collect = this.collectionContent.collect + 1
					}
					this.$func.showToast(res.msg)
				})
			},
			// 获取试卷详情
			getCollectionContent() {
				uni.showLoading({
					title: "试题努力加载中",
					icon: "none"
				})
				getCollectionContent({
					uid: this.collectionUid
				}).then(res => {
					uni.hideLoading()
					if (res.code == 100) {
						this.collectionContent = res.data
						this.swiperList = res.data.picture
						return
					}
					this.$func.showTotast(res.msg)
				})
			},
			onlineExam(uid) {
				getUserBasicInfoNoAuth().then(res => {
					if (res.code == 100) {
						this.tn("/subpages/exam/reading/submit?uid=" + uid)
					} else {
						this.$func.showToast("请先登录")
						this.tn("/subpages/user/login")
					}
				})

			},
			// 轮播图切换
			cardSwiper(e) {
				this.cardCur = e.detail.current
			},
			tn(e) {
				uni.navigateTo({
					url: e,
				})
			},
			// 微信订阅消息
			async subscribe() {
				let subscribeResult = await this.$func.templateSubscribe(getApp().globalData.examUpdateTemplateId)
				let code = subscribeResult.code || 0
				if (code == 100) {
					this.$func.showToast("订阅成功")
					return
				}
				this.$func.showToast("订阅失败")
			},
			// 获取顶部销售信息容器相关信息
			getTopInfoRect() {
				this._tGetRect('#top-info').then((res) => {
					if (!res) {
						setTimeout(() => {
							this.getTopInfoRect()
						}, 50)
						return
					}
					this.topInfo.height = res.height
				})
			},
			// 处理页面滚动事件
			handlePageScroll(scrollTop) {
				if (this.prevScrollTop > scrollTop) {
					// 内容下滑，即手指从下往上滚
					this.topInfoTranslateY = 0
				} else {
					// 内容上滑，即手指从上往下滚
					if (scrollTop > this.topInfo.height) {
						this.topInfoTranslateY = this.topInfo.height
					}
				}
				this.prevScrollTop = scrollTop
			},
		}
	}
</script>

<style lang="scss" scoped>
	.template-product {}

	.tn-tabbar-height {
		min-height: 20rpx;
		height: calc(40rpx + env(safe-area-inset-bottom) / 2);
	}

	/* 用户头像 start */
	.logo-image {
		width: 110rpx;
		height: 110rpx;
		position: relative;
	}

	.logo-pic {
		background-size: cover;
		background-repeat: no-repeat;
		// background-attachment:fixed;
		background-position: top;
		box-shadow: 0rpx 0rpx 80rpx 0rpx rgba(0, 0, 0, 0.15);
		border-radius: 10rpx;
		overflow: hidden;
		// background-color: #FFFFFF;
	}

	/* 胶囊*/
	.tn-custom-nav-bar__back {
		width: 100%;
		height: 100%;
		position: relative;
		display: flex;
		justify-content: space-evenly;
		align-items: center;
		box-sizing: border-box;
		background-color: rgba(0, 0, 0, 0.15);
		border-radius: 1000rpx;
		border: 1rpx solid rgba(255, 255, 255, 0.5);
		color: #FFFFFF;
		font-size: 18px;

		.icon {
			display: block;
			flex: 1;
			margin: auto;
			text-align: center;
		}

		&:before {
			content: " ";
			width: 1rpx;
			height: 110%;
			position: absolute;
			top: 22.5%;
			left: 0;
			right: 0;
			margin: auto;
			transform: scale(0.5);
			transform-origin: 0 0;
			pointer-events: none;
			box-sizing: border-box;
			opacity: 0.7;
			background-color: #FFFFFF;
		}
	}

	/* 轮播视觉差 start */
	.card-swiper {
		height: 750rpx !important;
	}

	.card-swiper swiper-item {
		width: 750rpx !important;
		left: 0rpx;
		box-sizing: border-box;
		// padding: 0rpx 30rpx 90rpx 30rpx;
		overflow: initial;
	}

	.card-swiper swiper-item .swiper-item {
		width: 100%;
		display: block;
		height: 100%;
		transform: scale(1);
		transition: all 0.2s ease-in 0s;
		overflow: hidden;
	}

	.card-swiper swiper-item.cur .swiper-item {
		transform: none;
		transition: all 0.2s ease-in 0s;
	}

	.image-banner {
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.image-banner image {
		width: 100%;
		height: 100%;
	}

	/* 轮播指示点 start*/
	.indication {
		z-index: 9999;
		width: 100%;
		height: 36rpx;
		position: absolute;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
	}

	.spot {
		background-color: #FFFFFF;
		opacity: 0.6;
		width: 10rpx;
		height: 10rpx;
		border-radius: 20rpx;
		top: -60rpx;
		margin: 0 8rpx !important;
		position: relative;
	}

	.spot.active {
		opacity: 1;
		width: 30rpx;
		background-color: #FFFFFF;
	}

	/* 间隔线 start*/
	.tn-strip-bottom-min {
		width: 100%;
		border-bottom: 1rpx solid #F8F9FB;
	}

	/* 间隔线 start*/
	.tn-strip-bottom {
		width: 100%;
		border-bottom: 20rpx solid rgba(241, 241, 241, 0.8);
	}

	/* 间隔线 end*/
	/* 标题 start */
	.nav_title {
		-webkit-background-clip: text;
		color: transparent;

		&--wrap {
			position: relative;
			display: flex;
			height: 120rpx;
			font-size: 46rpx;
			align-items: center;
			justify-content: center;
			font-weight: bold;
			background-image: url(https://tnuiimage.tnkjapp.com/title_bg/title44.png);
			background-size: cover;
		}
	}

	/* 标题 end */

	/* 用户头像 start */
	.user-image {
		width: 90rpx;
		height: 90rpx;
		position: relative;
	}

	.user-pic {
		background-size: cover;
		background-repeat: no-repeat;
		// background-attachment:fixed;
		background-position: top;
		border-radius: 50%;
		overflow: hidden;
		background-color: #FFFFFF;
	}

	/* 底部*/
	.tn-footerfixed {
		position: fixed;
		background-color: rgba(255, 255, 255, 0.5);
		box-shadow: 0rpx 0rpx 30rpx 0rpx rgba(0, 0, 0, 0.07);
		bottom: 0;
		width: 100%;
		transition: all 0.25s ease-out;
		will-change: transform;
		z-index: 100;
	}

	/* 底部 start*/
	.footerfixed {
		position: fixed;
		width: 100%;
		bottom: 0;
		z-index: 999;
		box-shadow: 0rpx 0rpx 30rpx 0rpx rgba(0, 0, 0, 0.07);
	}

	/* 标签内容 start*/
	.tn-tag-content {
		&__item {
			display: inline-block;
			line-height: 45rpx;
			padding: 10rpx 30rpx;
			margin: 20rpx 20rpx 5rpx 0rpx;

			&--prefix {
				padding-right: 10rpx;
			}
		}
	}

	/* 标签内容 end*/

	/* 内容图 start */
	.content-backgroup {
		z-index: -1;

		.backgroud-image {
			width: 100%;
		}
	}

	/* 内容图 end */

	/* 商家商品 start*/
	.tn-blogger-content {
		&__wrap {
			box-shadow: 0rpx 0rpx 50rpx 0rpx rgba(0, 0, 0, 0.07);
			border-radius: 20rpx;
			margin: 15rpx;
		}

		&__info {
			&__btn {
				margin-right: -12rpx;
				opacity: 0.5;
			}
		}

		&__label {
			&__item {
				line-height: 45rpx;
				padding: 0 10rpx;
				margin: 5rpx 18rpx 0 0;

				&--prefix {
					color: #E83A30;
					padding-right: 10rpx;
				}
			}

			&__desc {
				line-height: 35rpx;
			}
		}

		&__main-image {
			border-radius: 16rpx 16rpx 0 0;

			&--1 {
				max-width: 690rpx;
				min-width: 690rpx;
				max-height: 400rpx;
				min-height: 400rpx;
			}

			&--2 {
				max-width: 260rpx;
				max-height: 260rpx;
			}

			&--3 {
				height: 212rpx;
				width: 100%;
			}
		}

		&__count-icon {
			font-size: 24rpx;
			padding-right: 5rpx;
		}
	}

	.image-book {
		padding: 150rpx 0rpx;
		font-size: 16rpx;
		font-weight: 300;
		position: relative;
	}

	.image-picbook {
		background-size: cover;
		background-repeat: no-repeat;
		// background-attachment:fixed;
		background-position: top;
		border-radius: 15rpx 15rpx 0 0;
	}

	/* 按钮 */
	.button-1 {
		background-color: rgba(0, 0, 0, 0.15);
		position: absolute;
		/* bottom:200rpx;
      right: 20rpx; */
		top: 700rpx;
		right: 30rpx;
		z-index: 1001;
		border-radius: 100px;
	}

	.see {
		display: flex;
		justify-content: space-between;
		padding-top: 10rpx;
		border-radius: 6rpx;
		color: #666;
		line-height: 1.6;
	}
</style>